package org.se.scut.pinterest.mina.codec.filte.coder.decoder;

import java.io.InputStream;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.demux.MessageDecoderResult;
import org.se.scut.pinterest.mina.codec.coder.cxt.BaseContext;
import org.se.scut.pinterest.mina.codec.coder.cxt.PicturesPostContext;
import org.se.scut.pinterest.mina.codec.msg.PicturesPostMessage;
import org.se.scut.pinterest.mina.codec.msg.PicturesPostMessageItem;
import org.se.scut.pinterest.mina.utils.ListUtils;

public class PicturesPostDecoder extends BaseMessageDecoder {

	public PicturesPostDecoder() {
		super(new AttributeKey(PicturesPostDecoder.class, "context"), "ppr");
	}

	@Override
	public MessageDecoderResult decode(IoSession session, IoBuffer in,
			ProtocolDecoderOutput out) throws Exception {
		PicturesPostContext context = (PicturesPostContext)getContext(session);
		IoBuffer buffer = context.getInnerBuffer();
		int matchCount = context.getMatchCount();
		int lineNumber = context.getLineNumber();
		String symbol = context.getSymbol();
		String userID = context.getUserID();
		String ablumID = context.getAblumID();
		String pictureTypeID = context.getPictureTypeID();
		String from = context.getFrom();
		String count = context.getCount();
		String size = context.getSize();
		String itemId = context.getItemid();
		String itemFileName = context.getItemfileName();
		String itemOverView = context.getItemoverView();
		String itemNumOfCollected = context.getItemnumOfCollected();
		String itemCreateTime = context.getItemcreateTime();
		String itemPictureTypeID = context.getPictureTypeID();
		String fileLength = context.getItemfileLength();
		byte[] content = context.getItemcontent();
		byte b;
		while (in.hasRemaining()) {
			b = in.get();
			matchCount++;
			buffer.put(b);
			if (b == 10) {
				if (lineNumber == 0) {
					buffer.flip();
					symbol = buffer.getString(matchCount, DECODER);
					symbol = symbol.substring(0, symbol.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setSymbol(symbol);
				}
				if (lineNumber == 1) {
					buffer.flip();
					userID = buffer.getString(matchCount, DECODER);
					userID = userID.substring(0, userID.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setUserID(userID);
				}
				if (lineNumber == 2) {
					buffer.flip();
					ablumID = buffer.getString(matchCount, DECODER);
					ablumID = ablumID.substring(0, ablumID.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setAblumID(ablumID);
				}
				if (lineNumber == 3) {
					buffer.flip();
					pictureTypeID = buffer.getString(matchCount, DECODER);
					pictureTypeID = pictureTypeID.substring(0, pictureTypeID.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setPictureTypeID(pictureTypeID);
				}
				if (lineNumber == 4) {
					buffer.flip();
					from = buffer.getString(matchCount, DECODER);
					from = from.substring(0, from.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setFrom(from);
				}
				if (lineNumber == 5) {
					buffer.flip();
					count = buffer.getString(matchCount, DECODER);
					count = count.substring(0, count.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setCount(count);
				}
				if (lineNumber == 6) {
					buffer.flip();
					size = buffer.getString(matchCount, DECODER);
					size = size.substring(0, size.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setSize(size);
				}
				if ( lineNumber + 1 % 8 == 0
						&& lineNumber < 8 * Integer.parseInt(size) + 7
						 ) {
					buffer.flip();
					itemId = buffer.getString(matchCount, DECODER);
					itemId = itemId.substring(0, itemId.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setItemid(itemId);
				}
				if (lineNumber != 0 
						&& lineNumber  % 8 == 0
						&&lineNumber < 8 * Integer.parseInt(size) + 7
						 ) {
					buffer.flip();
					itemFileName = buffer.getString(matchCount, DECODER);
					itemFileName = itemFileName.substring(0, itemFileName.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setItemfileName(itemFileName);
				}
				if (lineNumber != 1
						&& lineNumber - 1 % 8 == 0
						&& lineNumber < 8 * Integer.parseInt(size) + 7 ) {
					buffer.flip();
					itemOverView = buffer.getString(matchCount, DECODER);
					itemOverView = itemOverView.substring(0, itemOverView.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setItemoverView(itemOverView);
				}
				if (lineNumber != 2
						&& lineNumber - 2 % 8 == 0
						&& lineNumber < 8 * Integer.parseInt(size) + 7 ) {
					buffer.flip();
					itemNumOfCollected = buffer.getString(matchCount, DECODER);
					itemNumOfCollected = itemNumOfCollected.substring(0, itemNumOfCollected.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setItemfileName(itemNumOfCollected);
				}
				if (lineNumber != 3
						&& lineNumber - 3 % 8 == 0
						&& lineNumber < 8 * Integer.parseInt(size) + 7 ) {
					buffer.flip();
					itemCreateTime = buffer.getString(matchCount, DECODER);
					itemCreateTime = itemCreateTime.substring(0, itemCreateTime.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setItemcreateTime(itemCreateTime);
				}
				if (lineNumber != 4
						&& lineNumber - 4 % 8 == 0
						&& lineNumber < 8 * Integer.parseInt(size) + 7) {
					buffer.flip();
					itemPictureTypeID = buffer.getString(matchCount, DECODER);
					itemPictureTypeID = itemPictureTypeID.substring(0, itemPictureTypeID.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setItempictureTypeID(itemPictureTypeID);
				}
				if (lineNumber != 5
						&& lineNumber - 5 % 8 == 0
						&& lineNumber < 8 * Integer.parseInt(size) + 7) {
					buffer.flip();
					fileLength = buffer.getString(matchCount, DECODER);
					fileLength = fileLength.substring(0, fileLength.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setItemfileLength(fileLength);
				}
				lineNumber++;
			}else if (lineNumber != 6
					&& lineNumber - 6 % 8 == 0
					&& lineNumber < 8 * Integer.parseInt(size) + 7 ) {
				if (matchCount == Integer.parseInt(fileLength)) {
					buffer.flip();
					InputStream inStream = buffer.asInputStream();
					content = new byte[Integer.parseInt(fileLength)];
					inStream.read(content);
					matchCount = 0;
					buffer.clear();
					context.setItemcontent(content);
					PicturesPostMessageItem item = new PicturesPostMessageItem();
					item.setContent(context.getItemcontent());
					item.setCreateTime(context.getItemcreateTime());
					item.setFileName(context.getItemfileName());
					item.setId(Integer.parseInt(context.getItemid()));
					item.setNumOfCollected(Integer.parseInt(context
							.getItemnumOfCollected()));
					item.setOverView(context.getItemoverView());
					item.setPictureTypeID(Integer.parseInt(context
							.getItempictureTypeID()));
					context.getItems().add(item);
					context.setLineNumber(lineNumber);
					if (size != null  
							&& lineNumber == 8 * Integer.parseInt(size) + 6
							&& matchCount == Integer.parseInt(fileLength)) {
						 break;
					}
				}
				
			}	
			context.setMatchCount(matchCount);
			context.setLineNumber(lineNumber);
		}
		if (context.getLineNumber() == 8* Integer.parseInt(size) + 6
				&& context.getMatchCount() == Integer.parseInt(context.getItemfileLength())) {
			PicturesPostMessage msg = new PicturesPostMessage();
			msg.setAblumID(Integer.parseInt(context.getAblumID()));
			msg.setCount(Integer.parseInt(context.getCount()));
			msg.setFrom(Integer.parseInt(context.getFrom()));
			msg.setPictureTypeID(Integer.parseInt(context.getPictureTypeID()));
			msg.setUserID(Integer.parseInt(context.getUserID()));
			ListUtils<PicturesPostMessageItem> utils = new ListUtils<PicturesPostMessageItem>();
			utils.copyList(msg.getItems(), context.getItems());
			out.write(msg);
			context.resetData();
			return MessageDecoderResult.OK;
		}
		else {
			return MessageDecoderResult.NEED_DATA;
		}
	}

	@Override
	public void finishDecode(IoSession session, ProtocolDecoderOutput out)
			throws Exception {

	}

	@Override
	public BaseContext getContext(IoSession session) {
		PicturesPostContext context = (PicturesPostContext)session.getAttribute(CONTEXT_KEY);
		if (context == null) {
			context = new PicturesPostContext();
			session.setAttribute(CONTEXT_KEY, context);
		}
		return context;
	}

}
